/*
 * Copyright (c) 2006-2021, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author       Notes
 * 2021-06-21     THEWON       first version
 */

#ifndef __DRV_NAND_H__
#define __DRV_NAND_H__

#include "board.h"

#ifdef __cplusplus
extern "C"
{
#endif

#ifndef RT_DEBUG_NAND
#define RT_DEBUG_NAND                   0
#endif

/* define your nand flash here */
#define MT29F4G08ABADA
//#define MT29F8G08ABACA
//#define MT29F16G08ABABA

#ifdef MT29F4G08ABADA
#define NAND_DEV_ID                     0x569590DC              /* Byte 4 3 2 1 */
#define NAND_MAX_PAGE_SIZE              2048
#elif defined(MT29F8G08ABACA)
#define NAND_DEV_ID                     0x64A690D3
#define NAND_MAX_PAGE_SIZE              4096
#elif defined(MT29F16G08ABABA)
#define NAND_DEV_ID                     0x89260048
#else
#error "Must define one NAND Flash device"
#endif

#define NAND_WAITRB_PIN                 GET_PIN(D, 6)

#define NAND_ECC_SECTOR_SIZE            512
#define NAND_SECTOR_SIZE                2048

#define NAND_tWHR_DELAY                 25
#define NAND_tBERS_DELAY                4

#define NAND_BASE_ADDR                  ((rt_uint32_t)0x80000000)                       /* nand base address */
#define NAND_DATA                       (*(__IO rt_uint8_t *)NAND_BASE_ADDR)
#define NAND_CMD                        (*(__IO rt_uint8_t *)(NAND_BASE_ADDR | 1 << 16))     /*  command */
#define NAND_ADDR                       (*(__IO rt_uint8_t *)(NAND_BASE_ADDR | 1 << 17))     /*  data */

/* nand flash command */
#define NAND_READID                     0x90
#define NAND_FEATURE                    0xEF
#define NAND_RESET                      0xFF
#define NAND_STATUS                     0x70

#define NAND_READ_MODE                  0x00
#define NAND_READ_PAGE                  0x30
#define NAND_PROG_MODE                  0x80
#define NAND_PROG_PAGE                  0x10
#define NAND_ERASE_MODE                 0x60
#define NAND_ERASE_BLOCK                0xD0

#define NAND_MOVEOUT_MODE               0x00
#define NAND_MOVEOUT_DATA               0x35
#define NAND_MOVEIN_MODE                0x85
#define NAND_MOVEIN_DATA                0x10

#ifdef __cplusplus
}
#endif

#endif
